﻿
#include "custombtn.h"
#include <QDebug>
customBtn::customBtn(QWidget *parent, QString text, QString iconFile)
    : QFrame(parent), m_color(QColor(255, 116, 0, 255)), m_iconFile(iconFile), m_btnText(text)
{
    this->initUI();
}

customBtn::~customBtn()
{

}

void customBtn::setColor(const QColor &color)
{
    if (m_color != color) {
        m_color = color;
        emit colorChanged();
    }
}

void customBtn::setSize(const QSize &size)
{
    if (m_size != size) {
        m_size = size;
        this->resize(size);
    }
}

void customBtn::hoveringAnimation()
{
    m_pAnimation->setTargetObject(this);        //动画的目标对象设置为当前对象
    m_pAnimation->setPropertyName("m_color");   //动画关联到属性m_color，这意味着动画将修改该属性的值来实现动画效果
    m_pAnimation->setDuration(300);             //动画的持续时间
    //动画的开始值和结束值
    m_pAnimation->setStartValue(QColor(255, 116, 0, 255));
    m_pAnimation->setEndValue(QColor(255, 116, 255, 225));
    m_pAnimation->start();
}

void customBtn::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
    //设置画笔; Qt::NoPen 表示不使用画笔，也就是不绘制边框
    painter.setPen(Qt::NoPen);

    // 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(this->rect(), 10, 10);
    painter.fillPath(path, m_color);
}

void customBtn::enterEvent(QEvent *event)
{
    // 设置动画的方向为正向
    m_pAnimation->setDirection(QAbstractAnimation::Forward);
    hoveringAnimation();
    QFrame::enterEvent(event);
}

void customBtn::leaveEvent(QEvent *event)
{
    //将动画设置为反向播放
    m_pAnimation->setDirection(QAbstractAnimation::Backward);
    this->hoveringAnimation();
    QFrame::leaveEvent(event);
}

void customBtn::updateColor()
{
    //按钮的背景颜色需要设置成透明,以便于绘制显示QFrame底色
    this->setStyleSheet(QString("QFrame{border-radius: 10px; background-color: rgba(255, 116, 200, 225);} "
                          "QPushButton{border-radius: 10px; background-color: transparent; color: rgb(255, 255, 255);}"));
    this->update();
}

void customBtn::initUI()
{
    this->resize(110, 30);
    this->setMinimumSize(40, 30);
    this->setMaximumSize(110, 30);
    m_size=this->size();
    //QFrame背景颜色和按钮的背景颜色一样,这样就看不见两个按钮之间的边框圆角
    this->setStyleSheet(QString("QFrame{border-radius: 10px; background-color: rgba(255, 116, 0, 255); color: rgb(255, 255, 255);} "
                          "QPushButton{border-radius: 10px; background-color: rgba(255, 116, 0, 255); color: rgb(255, 255, 255);}"));
    m_pPushButtonIcon = new QPushButton(this);
    m_pPushButtonIcon->setGeometry(QRect(0, 0, 30, 31));
    m_pPushButtonIcon->setStyleSheet("padding-left:10px");//设置左边内边距的大小
    QIcon icon(m_iconFile);
    m_pPushButtonIcon->setIcon(icon);
    m_pPushButtonIcon->setIconSize(QSize(20, 20));

    m_pPushButtonRight = new QPushButton(this);
    m_pPushButtonRight->setGeometry(QRect(30, 0, 80, 31));
    m_pPushButtonRight->setStyleSheet("padding-left:15px; text-align: left;");
    m_pPushButtonRight->setIconSize(QSize(20, 20));
    m_pPushButtonRight->setText(m_btnText);
    m_pPushButtonRight->setCheckable(true);
    connect(this,SIGNAL(colorChanged()), this, SLOT(updateColor()));

    m_pAnimation=new QPropertyAnimation(this);
}

